package com.example.myzhugezhaofang.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;

import com.example.myzhugezhaofang.R;
import com.example.myzhugezhaofang.adapter.WelcomeAdapter;

import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

public class StartActivity extends AppCompatActivity {

    private ViewPager mViewpager;
    private int time = 5;
    private Disposable disposable;
    private TextView tv_start;
    private Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message msg) {
            tv_start.setText("跳过：" + time);
            time--;
            return false;
        }
    });

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_start);
        mViewpager = findViewById(R.id.viewpager);
        initView();
    }

    private void initView() {
        ArrayList<View> views = new ArrayList<>();
        View v1 = LayoutInflater.from(this).inflate(R.layout.viewpager_item1, null);
        View v2 = LayoutInflater.from(this).inflate(R.layout.viewpager_item2, null);
        tv_start = v2.findViewById(R.id.tv_start);
        views.add(v1);
        views.add(v2);

        WelcomeAdapter welcomeAdapter = new WelcomeAdapter(views);
        mViewpager.setAdapter(welcomeAdapter);

        initFilter();
    }

    private void initFilter() {
        SharedPreferences a1 = getSharedPreferences("a", MODE_PRIVATE);
        boolean isFirst = a1.getBoolean("isFirst", true);
//        if (isFirst) {
//            SharedPreferences.Editor edit = a1.edit();
//            edit.putBoolean("isFirst", false);
//            edit.commit();

        disposable = Observable.interval(1, 1, TimeUnit.SECONDS)
                .subscribe(new Consumer<Long>() {
                    @Override
                    public void accept(Long aLong) throws Exception {
                        long l = time - aLong;
                        if (l >= 0) {
                            handler.sendEmptyMessage(0);
                        } else {
                            Intent intent = new Intent(StartActivity.this, MainActivity.class);
                            startActivity(intent);
                            disposable.dispose();
                        }
                    }
                });
//        } else {
//            Intent intent = new Intent(StartActivity.this, MainActivity.class);
//            startActivity(intent);
//        }
        tv_start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(StartActivity.this, MainActivity.class);
                startActivity(intent);
            }
        });
    }
}